package com.dailyblue.java.cloud.emp.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.java.cloud.commons.bean.Dept;
import com.dailyblue.java.cloud.commons.bean.Emp;
import com.dailyblue.java.cloud.commons.util.JsonResult;
import com.dailyblue.java.cloud.commons.util.ResultTool;
import com.dailyblue.java.cloud.emp.feign.DeptFeign;
import com.dailyblue.java.cloud.emp.mapper.EmpMapper;
import com.dailyblue.java.cloud.emp.service.EmpService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Resource
    private DeptFeign deptFeign;

    @Cacheable(cacheNames = "findEmpByPage", key = "#page")
    public JsonResult find(int page, int size) {
        PageHelper.startPage(page, size);
        List<Emp> list = list();
        // 拆箱装箱
        list.forEach(e -> {
            Dept dept = deptFeign.find(e.getDeptNo());
            e.setDept(dept);
        });
        PageInfo<Emp> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }
}
